/*!
 @header StatisticViewController.m
 @author 王 文彬
 @version 1.0.0 2013/01/24 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "StatisticViewController.h"
#import "Config.h"
#import "Helper.h"
#import "RTLabel.h"
#import "StatisticListViewController.h"
#import "JMFBase.h"


//  -----------------------类详细介绍-----------------------
//
//  <功能描述>：StatisticViewController功能较为简单，向服务器请求数据后，利用RTLabel显示出来即可，为完成点击事件，推荐与被推荐的两块区域以按钮的形式存在。
//
//  
//  -----------------------类升级日志-----------------------
//
//  修改人   修改时间        修改描述              版本
//
// <#修改人#> <#修改时间#>    <#修改描述#>          <#版本#>

@interface StatisticViewController ()

@end

@implementation StatisticViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.title = _L(@"vc_title_113");
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 372)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"recommend_bg_001@2x" ofType:@"png"]]];
    [self.view addSubview:bgView];
    
    //你推荐的
    UILabel* recommendLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 15, 100, 20)];
    recommendLabel.font = _SYS_FONT_004;
    recommendLabel.text = _L(@"title_statistic_03");

    [self.view addSubview:recommendLabel];
    
    //统计数据
    _recommendLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 65, 300, 120)];
    _recommendLabel.font = _SYS_FONT_004;
    _recommendLabel.text = [NSString stringWithFormat:RECOMMEND_COUNT_STRING, 0, 0, 0.00f, 0];
    _recommendLabel.textColor = _SYS_COLOR_002;
    
    [self.view addSubview:_recommendLabel];
    
    
    //按钮
    UIButton* recommendButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, 180)];
    [recommendButton addTarget:self action:@selector(recommendButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:recommendButton];
    
    
    //被推荐的
    UILabel* recommendYousLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 200, 100, 20)];
    recommendYousLabel.font = _SYS_FONT_004;
    recommendYousLabel.text = _L(@"title_statistic_04");

    [self.view addSubview:recommendYousLabel];
    
    //统计数据
    _recommendMineLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 245, 300, 120)];
    _recommendMineLabel.font = _SYS_FONT_004;
    _recommendMineLabel.textColor = _SYS_COLOR_002;
    _recommendMineLabel.text = [NSString stringWithFormat:RECOMMENDMINE_COUNT_STRING, 0, 0, 0, 0.00f];
    
    [self.view addSubview:_recommendMineLabel];
    
    //按钮
    UIButton* recommendMineButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 183, _SYS_FRAME_WIDTH, 180)];
    [recommendMineButton addTarget:self action:@selector(recommendMineButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:recommendMineButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// 视图将要载入时，刷新数据
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_RECOMMEND_STATISTICS];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_02");
    [request asynchronous];
}


#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*!
 @abstract 你推荐的按钮事件
 */
- (void)recommendButtonEvent
{
    if ( ! _recommendListViewController) {
        _recommendListViewController = [[StatisticListViewController alloc] init];
    }
    
    _recommendListViewController.recommendType = RecommendListTypeRecommend;
    _recommendListViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:_recommendListViewController animated:true];
}

/*!
 @abstract 被推荐的按钮事件
 */
- (void)recommendMineButtonEvent
{
    if ( ! _recommendListViewController) {
        _recommendListViewController = [[StatisticListViewController alloc] init];
    }
    
    _recommendListViewController.recommendType = RecommendListTypeBeRecomended;
    _recommendListViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:_recommendListViewController animated:true];
}


#pragma mark - JSONDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        NSDictionary* values = [dict valueForKey:@"data"];
        
        _recommendLabel.text = [NSString stringWithFormat:RECOMMEND_COUNT_STRING,
                                [[values valueForKey:@"recommendProductCount"] integerValue],
                                [[values valueForKey:@"recommendDealCount"] integerValue],
                                [[values valueForKey:@"recommendDealAmount"] floatValue],
                                [[values valueForKey:@"recommendPointsCount"] integerValue]];
        
        _recommendMineLabel.text = [NSString stringWithFormat:RECOMMENDMINE_COUNT_STRING,
                                    [[values valueForKey:@"beRecommendedProductCount"] integerValue],
                                    [[values valueForKey:@"beRecommendedCount"] integerValue],
                                    [[values valueForKey:@"beRecommendedDealCount"] integerValue],
                                    [[values valueForKey:@"beRecommendedDealAmount"] floatValue]];

    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}

@end
